<?php

namespace App\Http\Controllers\Admin;

use App\Models\Kind;
use App\Models\Notice;
use App\Models\Type;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

/**
 * 消息通知 管理
 * Class NoticeController
 * @package App\Http\Controllers\Admin
 */
class NoticeController extends BaseController
{
    /**
     * 消息列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $admin = $this->getAdmin();

        $list = $admin->notices()->orderByDesc('id')->paginate(config('site.other.paginate'));
        return backendView('notice.index', compact('list'));
    }

    /**
     * 已读消息列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function read(){
        $admin = $this->getAdmin();

        $list = $admin->notices()->orderByDesc('id')->wherePivot('status', 1)->paginate(config('site.other.paginate'));
        return backendView('notice.index', compact('list'));
    }

    /**
     * 未读消息列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function unread(){
        $admin = $this->getAdmin();

        $list = $admin->notices()->orderByDesc('id')->wherePivot('status', 0)->paginate(config('site.other.paginate'));
        return backendView('notice.index', compact('list'));
    }

    /**
     * 查看消息
     * @param null $id
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     */
    public function view($id = null)
    {
        /**
         * @var $kind Kind
         */
        $admin = $this->getAdmin();

        $notice = Notice::find($id);
        if (empty($notice)) {
            return back()->with('error', __('The page you requested was not found'));
        }

        if($admin->notices()->updateExistingPivot($id, ['status'=>1, 'read_at'=>date('Y-m-d H:i:s')])){
            Log::error("notice read error");
        }
        return backendView('notice.view', compact('notice'));
    }

    /**
     * 删除消息
     * @param null $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function delete($id = null)
    {
        /**
         * @var $kind Kind
         */
        $kind = Kind::find($id);
        if (empty($kind)) {
            return back()->with('error', __('The page you requested was not found'));
        }

        try {
/*            $users = $kind->users()->get();
            $devices = $kind->KindDevices()->get();
            //dd($games);
            Log::error($users);
            Log::error($devices);
            if (count($users) > 0 || count($devices) > 0) {
                return back()->with('error', '机构下有账户或设备，不能删除');
            }*/

            if ($kind->delete()) {
                return back()->with('success', __('Delete success'));
            }
        } catch (\Exception $e) {
            Log::error('Kind delete error:');
            Log::error($e->getMessage());
        }

        return back()->with('error', __('Delete failed'));
    }

    public function notify(){
        $admin = $this->getAdmin();
        $list = $admin->notices()->orderByDesc('id')->wherePivot('status', 0)->limit(5)->get();
        $list->each(function (&$item){
            $item->url = route("admin.notice.view", ['id'=>$item->id]);
        });

        return response()->json(['status'=>200, 'data'=>$list]);
    }
}